//
//  LoginViewController.m
//  Closer
//
//  Created by zhangkai on 16/3/10.
//  Copyright © 2016年 Closer_All. All rights reserved.
//

#import "LoginViewController.h"
#import "DisplayUserViewController.h"
#import "RegisterViewController.h"
#import "LeanCloudTools.h"
#import "User.h"



@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *usernameText;
@property (weak, nonatomic) IBOutlet UIView *usernameLine;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (strong, nonatomic) IBOutlet UIView *passwordLine;

- (IBAction)findLostPasswordButton:(UIButton *)sender;
- (IBAction)goRegisterButton:(UIButton *)sender;

- (IBAction)LoginButton:(UIButton *)sender;

- (IBAction)logOutButton:(UIButton *)sender;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}


#pragma mark - 找回密码
- (IBAction)findLostPasswordButton:(UIButton *)sender {
    
   
}
#pragma mark - 新用户
- (IBAction)goRegisterButton:(UIButton *)sender {
    RegisterViewController *registerViewController  = [[RegisterViewController alloc]init];
    [self presentViewController:registerViewController animated:YES completion:^{
    }];
}
#pragma mark - 登陆
- (IBAction)LoginButton:(UIButton *)sender {
    TypeOfLeanCloud flag=  [LeanCloudTools loginUserWith:self.usernameText.text and:self.passwordText.text];
    if (flag==userLoginSuccess) {
        [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.usernameText.text password:self.passwordText.text completion:^(NSDictionary *loginInfo, EMError *error) {
            if (!error && loginInfo) {
               
                [User shareInstance].username=self.usernameText.text;
                [User shareInstance].islogined=YES;
                 [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:YES];
                [self alertMessage:@"登陆成功"];
            }
        } onQueue:nil];
    }else{
        [self alertMessage:@"登录失败"];
    }
}
//注销
- (IBAction)logOutButton:(UIButton *)sender {
    
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES completion:^(NSDictionary *info, EMError *error) {
        if (!error && info) {
            [User shareInstance].islogined=NO;
            [User shareInstance].username=nil;
             [self alertMessage:@"退出成功"];
        }
        NSLog(@"%@",error);
    } onQueue:nil];
  }


-(void)alertMessage:(NSString*)message{
    UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alertController animated:YES completion:^{
        
        [self dismissViewControllerAnimated:YES completion:^{
            
            if ([message isEqualToString:@"登陆成功"]) {
            DisplayUserViewController *displayUserViewController=[[DisplayUserViewController alloc]init];
            [self  presentViewController:displayUserViewController animated:YES completion:^{
                
            }];
              }
        }];
    }];
}



@end
